package com.deyinet.bdcms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import com.deyinet.bdcms.core.vo.Page;
import com.deyinet.bdcms.entity.*;
import com.deyinet.bdcms.service.IAlarmServer;
import com.deyinet.bdcms.service.ILogsService;
import com.deyinet.bdcms.tools.RedisPoolUtils;
import com.deyinet.bdcms.tools.WebUtilify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import java.text.DateFormat;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/11 0011.
 */
@Controller
@RequestMapping(value = "/sysmanage/devicealarm")
public class DevicealarmController {
    @Autowired
    private IAlarmServer alarmServer;
    @Autowired
    private ILogsService logsService;

    @RequestMapping(value = "/alarmInfo")
    public void alarmInfo() {
    }
    @RequestMapping(value = "/getAlarmInfo")
    @ResponseBody
    public Map<String, Object> getAlarmInfo() {
        return alarmServer.getlastAlarm();
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public void list(String sn, Model model) {
        model.addAttribute("sn", sn);
    }

    @RequestMapping(value = "/listdata")
    @ResponseBody
    public String listdata(final HttpServletRequest request,Alarm alarm,Page<Alarm> page) throws ParseException {
      DateFormat format= new SimpleDateFormat("yyyy-MM-dd");
        String infos =request.getParameter("info");
        String createdatestart = request.getParameter("createdatestart");
        String createdateend = request.getParameter("createdateend");
        Timestamp ts=null;
        if (createdatestart!=null&&!"".equals(createdatestart)){
            ts=new Timestamp(format.parse(createdatestart).getTime());//string-->Timestamp
        }
        Timestamp te=null;
        if (createdateend!=null&&!"".equals(createdateend)){
            te=new Timestamp(format.parse(createdateend).getTime());//string-->Timestamp
        }
        if (infos!=null&&!"".equals(infos)){
          alarm.setInfo(infos);
        }

        alarmServer.findByExampleLike(alarm, page, ts, te);
        Map<String, Object> info = new HashMap<>();
        info.put("totalRow", page.getTotalRow());
        info.put("pageCurrent", page.getPageCurrent());
        info.put("list", page.getList());
        String ret = JSON.toJSONString(info, SerializerFeature.DisableCircularReferenceDetect);// JSONArray.toJSONString(datamodule);
        return ret;
    }
       // alarmServer.getPageByExampleEq(alarm,page);
       // return page;
    //}

    @RequestMapping(value = "/getAlarmNum")
    @ResponseBody
    public Map<String, Integer> getAlarmNum(){
        Map<String, Integer> info = new HashMap<>();
        alarmServer.getAlarmNum(info);
//        info.put("allAlarmNum",alarmServer.alarmCount(alarm));
//        info.put("deviceAlarmNum",alarmServer.deviceAlarmCount(alarm));
        return info;
    }

    @RequestMapping(value = "/flushAlarm")
    @ResponseBody
    public Map<String, String> flushAlarm(final HttpServletRequest request,Alarm alarm){
        alarmServer.flushAlarm(alarm.getSn());
        Map<String, String> info = new HashMap<>();
        info.put("message", "清空成功");
        info.put("content", "ok");
        return info;
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, Object> del(String json, HttpServletRequest request) {
        Syslog log = new Syslog();
        List<Alarm> alarms = JSONArray.parseArray(json, Alarm.class);
        String[] idarr = new String[alarms.size()];
        for (int i = 0; i < alarms.size(); i++) {
            idarr[i] = alarms.get(i).getSn();
        }
        alarmServer.batchDelete(idarr);
        log.setTitle("删除报警信息");
        Sysmodule sysmodule = new Sysmodule();
        sysmodule.setId(6);//根据数据库sysmodule表的对应的id去set
        log.setSysmoduleByModuleid(sysmodule);
        log.setSysuserByUserid(WebUtilify.LoginUserinfo(request));
        log.setContent(JSON.toJSONString(alarms));
        log.setCreatedate(new Timestamp(System.currentTimeMillis()));
        WebUtilify.WriteLog(request,log, logsService);

        Jedis redis = RedisPoolUtils.getJedisObject();
        redis.hdel(RedisPoolUtils.dbname,idarr);
        redis.close();
        Map<String, Object> info = new HashMap<>();
        info.put("message", "删除成功");
        info.put("status", 200);
        info.put("closeCurrent", "false");
        return info;
    }




}
